package com.ruoyi.ruoyixingyue.modules.api.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.ConvertUtils;
import com.ruoyi.common.core.utils.page.PageData;
import com.ruoyi.ruoyixingyue.modules.api.entity.Teacher;
import com.ruoyi.ruoyixingyue.modules.api.mapper.TeacherMapper;
import com.ruoyi.ruoyixingyue.modules.api.po.TeacherDictPo;
import com.ruoyi.ruoyixingyue.modules.api.po.TeacherListPo;
import com.ruoyi.ruoyixingyue.modules.api.service.ITeacherService;
import com.ruoyi.ruoyixingyue.modules.api.vo.TeacherInsertVo;
import com.ruoyi.ruoyixingyue.modules.api.vo.TeacherListVo;
import com.ruoyi.ruoyixingyue.modules.api.vo.TeacherStatusVo;
import com.ruoyi.ruoyixingyue.modules.api.vo.TeacherUpdateVo;
import com.ruoyi.ruoyixingyue.modules.utils.Condition;
import com.ruoyi.ruoyixingyue.modules.utils.QueryPage;
import com.ruoyi.ruoyixingyue.modules.utils.ValidatorUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 星月教师表
 * 服务实现类
 * </p>
 *
 * @author lzz
 * @since 2024-02-18
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements ITeacherService {

    @Override
    public R<PageData<TeacherListPo>> teacherListAPI_001(Condition<TeacherListVo> condition) {
        TeacherListVo teacherListVo = condition.getCondition();
        QueryPage queryPage = condition.getPage();
        Page<Teacher> teacherPage = new Page<Teacher>().setCurrent(queryPage.getCurrent()).setSize(queryPage.getSize());
        QueryWrapper<Teacher> queryWrapper = new QueryWrapper<>();

        //  进行查询
        List<Teacher> teacherList = this.list(queryWrapper.lambda()
                .orderByDesc(Teacher::getUpdateTime)
                .like(Objects.nonNull(teacherListVo.getUserName()), Teacher::getUserName, teacherListVo.getUserName())
                .like(Objects.nonNull(teacherListVo.getRealName()), Teacher::getRealName, teacherListVo.getRealName())
                .eq(Objects.nonNull(teacherListVo.getCellPhone()), Teacher::getCellPhone, teacherListVo.getCellPhone())
                .eq(Objects.nonNull(teacherListVo.getStatus()), Teacher::getStatus, teacherListVo.getStatus()));

        if (CollectionUtils.isEmpty(teacherList)) {
            return R.ok(new PageData<>(new ArrayList<TeacherListPo>(), 0));
        }

        List<TeacherListPo> resultList = new ArrayList<>();
        this.page(teacherPage, queryWrapper);
        teacherList.forEach(teacher -> {
            TeacherListPo target = ConvertUtils.sourceToTarget(teacher, TeacherListPo.class);
            resultList.add(target);
        });
        return R.ok(new PageData<>(resultList, teacherPage.getTotal()));
    }

    @Override
    public R<String> teacherUpdateStatusAPI_002(TeacherStatusVo teacherStatusVo) {
        ValidatorUtils.validateEntity(teacherStatusVo);
        Long teacherId = teacherStatusVo.getId();
        Integer status = teacherStatusVo.getStatus();
        List<Teacher> teacherList = this.list(new QueryWrapper<Teacher>().lambda().eq(Teacher::getId, teacherId));
        if (CollectionUtils.isEmpty(teacherList)) {
            throw new RuntimeException("教师表中没有改数据");
        }
        Teacher teacher = teacherList.get(0);
        teacher.setStatus(status);
        this.updateById(teacher);
        return R.ok(null, "修改成功");
    }

    @Override
    @Transactional
    public R<String> teacherInsertAPI_003(TeacherInsertVo teacherInsertVo) {
        ValidatorUtils.validateEntity(teacherInsertVo);
        String userName = teacherInsertVo.getUserName();
        String cellPhone = teacherInsertVo.getCellPhone();
        List<Teacher> validList1 = this.list(new QueryWrapper<Teacher>().lambda().eq(Teacher::getUserName, userName));
        if (!CollectionUtils.isEmpty(validList1)) {
            throw new RuntimeException("当前用户昵称已重复");
        }
        List<Teacher> validList2 = this.list(new QueryWrapper<Teacher>().lambda().eq(Teacher::getCellPhone, cellPhone));
        if (!CollectionUtils.isEmpty(validList2)) {
            throw new RuntimeException("当前手机号已重复");
        }
        Teacher teacher = new Teacher();
        BeanUtils.copyProperties(teacherInsertVo, teacher);
        teacher.setCreateTime(LocalDateTime.now());
        teacher.setUpdateTime(LocalDateTime.now());
        this.save(teacher);
        return R.ok(null, "操作成功");
    }

    @Override
    public R<TeacherListPo> teacherUpdateAPI_004(Long id) {
        Teacher teacher = this.getById(id);
        TeacherListPo target = ConvertUtils.sourceToTarget(teacher, TeacherListPo.class);
        return R.ok(target);
    }

    @Override
    public R teacherUpdateAPI_005(TeacherUpdateVo teacherUpdateVo) {
        Long id = teacherUpdateVo.getId();
        Teacher teacher = this.getById(id);
        teacher.setUserName(teacherUpdateVo.getUserName());
        teacher.setRealName(teacherUpdateVo.getRealName());
        teacher.setPassword(teacherUpdateVo.getPassword());
        teacher.setCellPhone(teacherUpdateVo.getCellPhone());
        teacher.setStatus(teacherUpdateVo.getStatus());
        this.updateById(teacher);
        return R.ok(null, "更新成功");
    }

    @Override
    public R teacherDeleteAPI_006(Long id) {
        List<Teacher> teacherList = this.list(new QueryWrapper<Teacher>().lambda().eq(Teacher::getId, id));
        if (CollectionUtils.isEmpty(teacherList)) {
            throw new RuntimeException("当前数据库中没有该教师");
        }
        Teacher teacher = teacherList.get(0);
        this.removeById(teacher);
        return R.ok(null, "删除成功");
    }

    /**
     * 教师字典接口
     */
    @Override
    public R teacherDictAPI_007() {
        List<Teacher> teacherList = this.list(new QueryWrapper<Teacher>());
        List<TeacherDictPo> target = ConvertUtils.sourceToTarget(teacherList, TeacherDictPo.class);
        return R.ok(target, "教师字典接口调用成功");
    }
}
